﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#ifndef __MAIN_HANDLER_H__
#define __MAIN_HANDLER_H__

#pragma once

#include "ServerConsole.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 服务加载器管理器
class CMainHandler : public CEventHandler
{
public:
    CMainHandler(void);
    virtual ~CMainHandler(void);

    bool    Init(void);
    void    Exit(void);
    // CEventHandler
    virtual UInt OnHandle(Int nEvent, uintptr_t utData, ULLong ullParam) OVERRIDE;
    virtual UInt OnHandle(Int nEvent, CEventBase& pParam, ULLong ullParam) OVERRIDE;
    virtual UInt OnHandle(Int nEvent, CStream& Stream, ULLong ullParam) OVERRIDE;
    virtual UInt OnHandle(Int nEvent, UInt uCount) OVERRIDE;
private:
    bool LoadServerHandlers(void);
    void UpdateServerHandlers(bool bStart = true);
    void UnloadServerHandlers(void);

    bool CreateServerHandler(Int nIndex);
    bool LoadServerHandler(Int nLoad, Int nIndex);
private:
    UInt              m_uStart;
    UInt              m_uLoad;
#if (__PLATFORM_TARGET__ == PLATFORM_TARGET_WINDOWS)
    CSyncEvent        m_hBusyLoad;
    CSyncEvent        m_hOverLoad;
#else
    CSyncSema         m_hBusyLoad;
    CSyncSema         m_hOverLoad;
#endif
    CCommonHandler*   m_pSHandler[PAKT_MAX];
};

#endif // __MAIN_HANDLER_H__
